using Microsoft.EntityFrameworkCore;
using Project_Schedule.Configures;
using Project_Schedule.Domain.Receptionist.com.core.dto;
using Project_Schedule.Domain.Receptionist.com.core.Interface;
using Project_Schedule.Domain.Receptionist.com.core.utils;
using Project_Schedule.Domain.Request;
using Project_Schedule.Model;
using Project_Schedule.Model.EntitiesHelper;

namespace Project_Schedule.Domain.Receptionist.com.core.Service;

public class System_ChangeWebRegisterUser : ISystemChangeRefuseUser
{
    public async Task<JRequest> ChangeMessage(RegisterUser dto)
    {
        try
        {
            await using var db = new DataDbContext();
            var info = await db.SystemUsers?.FirstOrDefaultAsync(e =>
                e.ErrorCode == dto.ErrorCode);
            if (info is null) throw new Exception("没有查询到此账户");
            if (!System_RegisterTools.RegisterCheckout(dto, out var message)) throw new Exception(message);
            info.Account = dto.Account;
            info.Name = dto.Name;
            info.Password = RsaEncryptUtil.RSAEncrypt(dto.Password);
            info.CreateTime = DateTime.Now;
            info.Post = dto.Post;
            info.Header = dto.Header;
            info.Sex = (Schedule_User_Sex_Enum)dto.Sex;
            info.Phone = dto.Phone;
            info.Email = dto.Email;
            info.Status = Schedule_User_State_Enum.On;
            info.IsUsed = System_Audit_Status_Enum.None;
            info.ErrorCode = null;
            await db.SaveChangesAsync();
            return JRequest.Ok(true);
        }
        catch (Exception exception)
        {
            _logHelper.LogError(exception);
            return JRequest.Ok(false, false, exception.Message);
        }
    }

    private readonly INLogHelper _logHelper;

    public System_ChangeWebRegisterUser(INLogHelper logHelper)
    {
        _logHelper = logHelper;
    }
}